<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用电数据展示</title>
    <!-- 引入 echarts.js -->
    <script src="https://cdn.bootcss.com/echarts/4.2.1-rc1/echarts.min.js"></script>
    <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
    <style>
        .view{
            float: left;
        }
    </style>
</head>
<body>
<!-- 为ECharts准备一个具备大小（宽高）的Dom -->
<div style="height: 100px">
    <button onclick="sendMsg()">发送登录包</button>
</div>
<div id="电压"    class="view" style="width: 600px;height:400px;"></div>
<div id="温度"    class="view" style="width: 600px;height:400px;"></div>
<div id="电流"    class="view" style="width: 600px;height:400px;"></div>
<div id="功率因数" class="view" style="width: 600px;height:400px;"></div>
<div id="有功功率" class="view" style="width: 600px;height:400px;"></div>
<div id="漏电数据" class="view" style="width: 600px;height:400px;"></div>
<script type="text/javascript">
    $(function () {
        //基于准备好的dom，初始化echarts实例
        $.get("/showData",function (res) {
            setFor(res)
        })
    })
    function sendMsg() {
        let val=$("#msg").val()
        $.post("/sendMsg",{msg:val},function (res) {
            alert("发送成功")
        })
    }
    function setFor(res) {
        Object.keys(res).forEach(function (value) {
            setData(value,res)
        })
    }
    function setData(value,res) {
        let tval=[]
        let t=res[value].map(it=> {return it.title})
        let keys=Object.keys(t)
        keys.forEach(function (value1) {
            tval.push(t[value1])
        })
        console.log(tval)
        let vals=[]
        let vval=res[value].map(it=> {return it.val})
        let vkeys=Object.keys(vval)
        vkeys.forEach(function (value1) {
            vals.push(vval[value1])
        })
        console.log(vals)
        var myChart = echarts.init(document.getElementById(value));

        // 指定图表的配置项和数据
        var option = {
            title: {
                text: value
            },
            tooltip: {},
            legend: {
                data:['数值']
            },
            xAxis: {
                data: tval
            },
            yAxis: {},
            series: [{
                name: '数值',
                type: 'bar',
                data: vals
            }]
        };

        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
    }



</script>
</body>
</html>